package operator

import (
	"gitee.com/bupt-zkjc/fabric2.2-interface/config_yaml"
	"gitee.com/bupt-zkjc/fabric2.2-interface/constant"
	"gitee.com/bupt-zkjc/fabric2.2-interface/entity"
	fautil "gitee.com/bupt-zkjc/fabric2.2-interface/util"
	"path/filepath"
)

type OrgOperator struct {
}

func (l *OrgOperator) AddNewOrg(org entity.Org) (string, error) {

	// Todo 使用脚本更新sdk配置文件，添加新节点的所有信息 addPeers.sh 配置文件 节点名称 节点URL
	path, err := config_yaml.GetParentDirectory()
	command :=  constant.AddOrg + constant.Separator + "config_" + org.ChainName + constant.Yaml + constant.Separator + org.OrgName
	err = fautil.Exec_shell(command, filepath.Join(path, constant.Dirpath))
	if err != nil{
		logger.Error(err)
		return "Update yaml file failure", err
	}

	logger.Info("SDK---Add new org success!")
	return "Add new org success", nil
}

func (l *OrgOperator) RemoveOrg(org entity.Org) (string, error) {

	path, err := config_yaml.GetParentDirectory()
	command :=  constant.RemoveOrg + constant.Separator + "config_" + org.ChainName + constant.Yaml +
		constant.Separator + org.OrgName
	err = fautil.Exec_shell(command, filepath.Join(path, constant.Dirpath))
	if err != nil{
		logger.Error(err)
		return "Update yaml file failure", err
	}

	logger.Info("SDK---Remove org success!")
	return "Remove org success", nil
}
